package services

import java.io.File

import dao.GermDao
import javax.inject._
import play.Logger
import play.api.libs.json.{JsObject, Json}
import utils.TableUtils

import scala.concurrent.Await
import scala.concurrent.duration.Duration

@Singleton
class OnStart @Inject()(germDao: GermDao) {


  Logger.info("开启成功！")


  TableUtils.treeData = getTreeData

  Logger.info("分类学树初始化成功")


  def getTreeData: Seq[JsObject] = {
    val germs = Await.result(germDao.getAllGerm, Duration.Inf)
    germs.groupBy(_.classes).map { case (classes, gc) =>
      Json.obj("text" -> classes, "tags" -> Seq(gc.length), "nodes" ->
        gc.groupBy(_.family).map { case (family, gf) =>
          Json.obj("text" -> (gf.head.familyLatin + " " + family), "tags" -> Seq(gf.length), "nodes" ->
            gf.groupBy(_.genus).map { case (genus, gg) =>
              Json.obj("text" -> (gg.head.genusLatin + " " + genus), "tags" -> Seq(gg.length), "nodes" ->
                gg.map { x =>
                  Json.obj("text" -> s"""<a href="javascript:void(0);" onclick="toGermInfo('${x.id}')" target='_blank'>${x.name}</a>""" )
                }
              )
            })

        })
    }.toSeq
  }
}



